package com.drug.controller;

import com.drug.domain.DrugAtegory;
import com.drug.domain.DrugInformation;
import com.drug.domain.PurchaseOrder;
import com.drug.domain.Result;
import com.drug.service.PurchaseService;
import com.drug.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import com.drug.service.PurchaseOrderService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.stereotype.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    @Autowired
    private PurchaseOrderService purchaseOrderService;
    /**
     *
     * 根据条件分页查询所有或多条件查询
     * @param map
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/findConditional")
    private Result findConditional(@RequestParam Map<String,Object> map,@RequestParam(required=true,defaultValue="1")Integer pageNum){
        return  purchaseService.findConditional(map,pageNum);
    }


    /**
     *
     * 根据条件分页查询所有或多条件查询
     * @param map
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/findDIa")
    private Result findDI(@RequestParam Map<String,Object> map,@RequestParam(required=true,defaultValue="1")Integer pageNum){
        return  purchaseService.findDI(map,pageNum);
    }

    /**
     * 采购目录维护页面展示
     * @param map
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/findDIaa")
    private Result findDI1(@RequestParam Map<String,Object> map,@RequestParam(required=true,defaultValue="1")Integer pageNum){
        return  purchaseService.findDI1(map,pageNum);
    }

    /**
     * 向采购目录添加药品
     * @param id
     * @return
     */
    @RequestMapping("/findadd")
    @ResponseBody
    private Result findadd(Integer[] id){
        return purchaseService.findPhAdd(id);
    }

    /**
     * 向采购目录删除药品
     * @param id
     * @return
     */
    @RequestMapping("/finddelect")
    @ResponseBody
    private Result finddelect(@RequestParam Integer[] id,@RequestParam Integer v){
        return purchaseService.findPhDelect(id,v);
    }
    /**
     * 向退货单删除药品
     * @param id
     * @return
     */
    @RequestMapping("/finddelect1")
    @ResponseBody
    private Result finddelect1(@RequestParam Integer[] id,@RequestParam Integer v){
        return purchaseService.findPhDelect1(id,v);
    }
    /**
     * 导出报表
     * @return
     */

    @RequestMapping(value = "/export")
    public String export(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String message = "";

        //获取数据
        List<DrugInformation> list = purchaseService.findAll1();

        //excel标题
        String[] title = {"序号","药品流水号","通用名","剂型","规格","单位","转换系数","生产企业","商品名称","中标价","药品类别","供货商","交易状态"};

        //excel文件名
        String fileName = "sampleInfo"+System.currentTimeMillis()+".xls";

        //sheet名
        String sheetName = "学生信息表";

        String [][] content = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            DrugInformation stu = list.get(i);

            content[i][0] = stu.getDiId()+"";
            content[i][1] = stu.getDiNumer()+"";
            content[i][2] = stu.getDiName()+"";
            content[i][3] = stu.getDiForm()+"";
            content[i][4] = stu.getDiSpecifications()+"";
            content[i][5] = stu.getDiUnit()+"";
            content[i][6] = stu.getDiCc()+"";
            content[i][7] = stu.getDrugAtegory().getDaSupplier()+"";
            content[i][8] = stu.getDrugAtegory().getDaName()+"";
            content[i][9] = stu.getDiNewPrice()+"";
            content[i][10] = stu.getDrugAtegory().getDaMName()+"";
            content[i][11] = stu.getDrugCategory().getDcName()+"";
            content[i][12] = stu.getDiTradingStatus()+"";

        }

        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
            message = "SUCCESS";
        } catch (Exception e) {
            e.printStackTrace();
            message = "ERROR";
        }
        return null;
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"utf-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
	
	/**
     * 查询全部分页
     */
    @ResponseBody
    @RequestMapping(value = "/PurchaseOrderController/findAll", produces = "application/json; charset=utf-8")
    private Result findConditional1(@RequestParam Map<String,Object> map, @RequestParam(required=true,defaultValue="1")Integer pageNum) throws ParseException {
        return  purchaseOrderService.selectPurchaseOrder(map,pageNum);
    }


    /**
     * 修改(提交审核结果)
     * @param
     * @return
     */
    @RequestMapping(value="/DrugInformationController/updateSubmit", produces = "application/json; charset=utf-8")
    @ResponseBody
    public Result updateSubmit(@RequestParam String[] poAuditResult,@RequestParam Integer[] poId,@RequestParam String[]poAuditOpinion){
        Result result = new Result();
        PurchaseOrder purchaseOrder= new PurchaseOrder();
        for(int i = 0;i<poAuditResult.length;i++){
            Integer integer = poId[i];
            purchaseOrder.setPoId(integer);
            String str = poAuditResult[i];
            purchaseOrder.setPoAuditResult(str);
            String s = poAuditOpinion[i];
            purchaseOrder.setPoAuditOpinion(s);
            if ("0".equals(poAuditResult[i])){
                purchaseOrder.setPoState("1");
            }else if ("1".equals(poAuditResult[i])){
                purchaseOrder.setPoState("2");
            }//改变审核结果的时候同时改变采购单状态
            result = purchaseOrderService.updateSubmit(purchaseOrder);

        }
        return result;
    }

}
